
/*
 * @Description: 
 * @Author: zml
 * @Date: 2025-07-28 12:08:14
 * @LastEditors: zml
 * @LastEditTime: 2025-08-13 14:36:18
 */

import type { FormProps } from 'antd';
import { Button, Form, Input, Spin } from 'antd';
import { useNavigate } from 'react-router-dom';
import { login } from '../../api/userInfo'
import { useState } from 'react';
import { useDispatch } from 'react-redux';
import { logins } from '../../store/modules/user_data';

type FieldType = {
  username?: string;
  password?: string;
  remember?: string;
};

function Login() {
  const navigate = useNavigate();
  const dispatch = useDispatch();
  const [spinning, setSpinning] = useState(false);
  const onFinish: FormProps<FieldType>['onFinish'] = async (values) => {
    setSpinning(true)
    const res: any = await login(values);
    console.log(res)
    if (res.code == 200) {
      dispatch(logins(res.userinfo));
      navigate("/", { replace: true }); // 替换历史记录
    }
    setSpinning(false)
  };

  const adds = () => {
    console.log(getThisSunday());
  }

  return (
    <div>
      <Form
        name="basic"
        layout="vertical"
        labelCol={{ span: 4 }}
        wrapperCol={{ span: 10 }}
        style={{ maxWidth: 600 }}
        initialValues={{ remember: true }}
        onFinish={onFinish}
        autoComplete="off"
      >
        <Form.Item<FieldType>
          label="用户名"
          name="username"
          rules={[{ required: true, message: '请输入你的用户名!' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item<FieldType>
          label="密码"
          name="password"
          rules={[{ required: true, message: '请输入您的密码!' }]}
        >
          <Input.Password />
        </Form.Item>

        <Form.Item >
          <Button type="primary" htmlType="submit">
            登录
          </Button>
        </Form.Item>
      </Form>
      <Button onClick={adds}>点击其他</Button>
      <Spin spinning={spinning} fullscreen />
    </div>
  )
}

export default Login